<?php

namespace app\admin\controller\setting;

use app\admin\controller\AuthController;
use service\FormBuilder as Form;
use service\JsonService;
use service\UtilService as Util;
use service\JsonService as Json;
use think\Exception;
use think\Request;
use app\admin\model\system\SystemRole;
use think\Url;
use app\admin\model\system\SystemAdmin as AdminModel;
use app\common\model\School;
use app\common\model\Organ;

/**
 * 管理员列表控制器
 * Class SystemAdmin
 * @package app\admin\controller\system
 */
class SystemAdmin extends AuthController
{
    public $model = '\app\admin\model\system\SystemAdmin';
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $admin = $this->adminInfo;
        $this->assign('role',SystemRole::getRole(bcadd($admin['level'],1,0)));
        return $this->fetch();
    }
    /**
     * 获取列表
     */
    public function get_list(){
        $where = $this->request->get();
        $where['page']  = $where['page'] ?? 1;
        $where['limit'] = $where['limit'] ?? 20;
        return JsonService::successlayui(AdminModel::getList($where));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data = $this->request->post();
        try {

            if(isset($data['pwd']) && (!empty($data['pwd']))){
                $data['pwd'] = md5($data['pwd']);
            }else{
                unset($data['pwd']);
            }
            $data['roles'] = implode(',',$data['roles']);
            if ( isset($data['id']) && $data['id'] > 0 ) {
                $res     = $this->model::update($data);
                $data_id = $data['id'];
            }else {
                if(AdminModel::be($data['account'],'account')) return Json::fail('管理员账号已存在');
                $res     = $this->model::create($data);
            }
            if ( $res !== false ) {
                return JsonService::successful('操作成功!');
            }
        }catch(\Exception $e){
            return JsonService::fail($e->getMessage());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!$id)
            return JsonService::fail('删除失败!');
        if(AdminModel::edit(['is_del'=>1,'status'=>0],$id,'id'))
            return JsonService::successful('删除成功!');
        else
            return JsonService::fail('删除失败!');
    }

    /**
     * 个人资料 展示
     * */
    public function adminInfo(){
        $adminInfo = $this->adminInfo;//获取当前登录的管理员
        $this->assign('adminInfo',$adminInfo);
        return $this->fetch();
    }

    public function setAdminInfo(Request $request){
        $adminInfo = $this->adminInfo;//获取当前登录的管理员
        if($request->isPost()){
            $data = Util::postMore([
                ['new_pwd',''],
                ['new_pwd_ok',''],
                ['pwd',''],
                'real_name',
            ],$request);
//            if ($data['pwd'] == '') unset($data['pwd']);
            if($data['pwd'] != ''){
                $pwd = md5($data['pwd']);
                if($adminInfo['pwd'] != $pwd) return Json::fail('原始密码错误');
            }
            if($data['new_pwd'] != ''){
                if(!$data['new_pwd_ok']) return Json::fail('请输入确认新密码');
                if($data['new_pwd'] != $data['new_pwd_ok']) return Json::fail('俩次密码不一样');
            }
            if($data['pwd'] != '' && $data['new_pwd'] != ''){
                $data['pwd'] = md5($data['new_pwd']);
            }else{
                unset($data['pwd']);
            }
            unset($data['new_pwd']);
            unset($data['new_pwd_ok']);
            AdminModel::edit($data,$adminInfo['id']);
            return Json::successful('修改成功!,请重新登录');
        }
    }
    /**
     * 更改的状态
     */
    public function set_status($status = '', $id = '' , $field = 'status')
    {
        if ( $this->model == null ) {
            return JsonService::fail('模型未初始化');
        }

        if ($status == '' || $id == '') return JsonService::fail('缺少参数');

        $res = $this->model::where(['id' => $id])->update([$field => (int)$status]);
        if ($res !== false ) {
            return JsonService::successful($status == 1 ? '显示成功' : '隐藏成功');
        } else {
            return JsonService::fail($status == 1 ? '显示失败' : '隐藏失败');
        }
    }
}
